import Config from './config'
class Base{
	constructor() {
	    this.img_url = Config.img_url
		this.base_qequest_url = Config.requset_url; //api接口基础地址
		this.base_qequest_url_user = Config.requset_url_user; //接口地址
		this.base_wx_login_url = Config.wx_login_url;
		this.base_wx_order_pay_url = Config.wx_order_pay_url; //支付地址
		this.apikey = Config.apikey;
	}
	//不带权限的验证
	requsetGet(params) {
		var that=this;
		let url=that.base_qequest_url_user+params.url;
		let data = params.data
		for (let key in data) {
			if (data[key] === '' || data[key] === null || data[key] === undefined) {
				delete data[key]
			}
		}
		uni.request({
			url:url,
			method:params.method || 'GET',
			data:data || {},
			success: (res) => {
				//return res;
				params.sCallBack && params.sCallBack(res);
				return;
			},
			fail: (err) => {
				console.log(err,'==报错信息==');
			}
		})
	}
	//带权限的验证
	requestUser(params) {
		var that=this;
		let url=that.base_qequest_url_user + params.url;
		let data = params.data
		for (let key in data) {
			if (data[key] === '' || data[key] === null || data[key] === undefined) {
				delete data[key]
			}
		}
		uni.request({
			url:url,
			method:params.method || 'GET',
			data:data || {},
			header: {
				// 'Access-Sign':sign||"123456",
				//'Content-Type':'application/x-www-form-urlencoded; charset=utf-8',
				'X-Access-Token':uni.getStorageSync('token'),
				'Login-Type':'client',
				'c_type':'mini'
			},
			success: (res) => {
				// return res;
				params.sCallBack && params.sCallBack(res);
				return;
			},
			fail: (err) => {
				console.log(err,'==报错信息==');
			}
		})
	}
	//跳转tab
	switch_tab(url) {
		var that=this;
		//that.show_loading('加载中...')
		uni.switchTab({
			url: url
		})
	}
	//提示
	show_loading(message) {
		if (uni.showLoading) {
			// 基础库 1.1.0 微信6.5.6版本开始支持，低版本需做兼容处理
			uni.showLoading({
				title: message,
				mask: true
			});
		} else {
			// 低版本采用Toast兼容处理并将时间设为20秒以免自动消失
			uni.showToast({
				title: message,
				icon: 'loading',
				mask: true,
				duration: 20000
			});
		}
	}
	reLaunch(url){
		var that=this;
		//that.show_loading('加载中...')
		uni.reLaunch({
			url: url
		});
	}
	redirectto(url) {
		var that=this;
		//that.show_loading('加载中...')
		uni.redirectTo({
			url:url
		})
	}
	//跳转页面
	navigate_to(url) {
		var that=this;
		//that.show_loading('加载中...')
		uni.navigateTo({
			url: url,
			animationType: 'pop-in',
			animationDuration: 200
		})
	}
	//页面返回
	navigate_back(delta) {
		uni.navigateBack({
			delta: delta || 1,
		});
	}
	//弹出框
	show_modal(params, callBack) {
		var that=this;
		that.show_loading('加载中...')
		uni.showModal({
			title: params.title || '提示',
			content: params.content || '网络错误...',
			showCancel: params.showCancel === false ? false : true,
			cancelText: params.cancelText || '取消',
			success: function(res) {
				callBack(res);
			}
		});
	}
	//获取input的值
	get_input_val(event) {
		return event.target.value;
	};
	//数组去重
	unique_arr(arr) {
		var hash = [];
		for (var i = 0; i < arr.length; i++) {
			if (hash.indexOf(arr[i]) == -1) {
				hash.push(arr[i]);
			}
		}
		return hash;
	}
	//判断是否为手机号
	is_phone_number(tel) {
		var reg = /^1[3|4|5|6|7|8|9][0-9]{9}$/;
		return reg.test(tel);
	}
	//判断验证码是否正确
	is_code_number(code) {
		var reg = /^[0-9]{6}$/;
		return reg.test(code);
	}
	
	//提示框
	show_tips(options) {
		uni.showToast({
			title: options || '加载中...',
			icon: 'none',
			duration: 2000
		});
	}
	//根据下标删除数组元素
	delete_arr_index(arr, index) {
		arr.splice(index, 1);
		return arr;
	}
	//从外链中 获取参数
	UrlParamHash(url) {
		var params = [],
		h;
		var hash = url.slice(url.indexOf("?") + 1).split('&');
		for (var i = 0; i < hash.length; i++) {
			h = hash[i].split("="); //
			params[h[0]] = h[1];
		}
		return params;
	}
	strTrim(str,char){
		if(!str){
			return false;
		}
		let r=str.substr(-1),l=str.substr(0,1);
		if(l==char){
			str=str.substr(1);
		}
		if(r==char){
			str=str.substr(0,str.length-1);
		}
		
		return str;
	}
	strRep(str,char,rchar){
		let reg=new RegExp(char,"g"); //创建正则RegExp对象   		
		str=str.replace(reg,rchar);
		return str
	}
	//从外链中 获取参数
	UrlParamHash(url) {
		var params = [],
		h;
		var hash = url.slice(url.indexOf("?") + 1).split('&');
		for (var i = 0; i < hash.length; i++) {
			h = hash[i].split("="); //
			params[h[0]] = h[1];
		}
		return params;
	}
}
export default Base
